function ready() {
	// 加载真正的博文数据,这样做的目的：为了解决网速慢的情况，这样做可以做真正的动态加载数据
	$.post(ctx+"/common/RequestBlog.jsp", {
		"actionSerial" : 0,
		"description" : "第一次加载首页面获取的数据"
	}, function(data) {
		$("#feed-list").html(data);
		avatarInfo();
	});

	// 下面我们做一个 每当页面到达页底的时候自动加载数据，效果类似于 新浪微博
	$(window).bind(
			'scroll',
			function() {
				if ($(window).scrollTop() + $(window).height() >= $(document)
						.height()) { // 如果到达页底的话，两者应该相等

					// 获取当前载入数据中，博文最旧id 其实就是Id最小的博文
					var lastArticleId = $(".feed-photo:last-child").attr(
							"data-articleid");

					$.post(ctx+"/common/RequestBlog.jsp", {
						"actionSerial" : 1,
						"count" : 3,
						"form" : "index",
						"lastArticleId" : lastArticleId,
						"description" : "获取首页更多条数据"
					}, function(data) {
						if ((/pop-content/g).test(data))
							$("#feed-list").append(data);
						avatarInfo();
					});
				}
			});

	$('#banners').bannerize({ // 最新图片浏览设置
		shuffle : 5,
		interval : "5"
	});


	//头像上传组件
	$(".publishlink-user").live("click", function() {

		$("#avatar-upload-dialog").dialog({
			resizable : true,
			modal : true,
			width:"470px",
			buttons: {
	               "确定上传": function() {
	            	   var fileId = $("#data-avatar-hidden").attr("data-avatar-hidden");
	            	   $.post(
	            			   ctx+"/user/file.action",
								{
	            				   "fileId" : fileId,
	            				   "method:changeAvatar":"xxxx"   
								},
							function(data) {
							   //对于头像进行数据的局部刷新（          对于Ajax过来的数据就不太好控制     ）
							   $(".publishlink-user  img").each(function(i){   //这个方式只有两种
							        this.src = ctx+"/user/file.action?method:previewPic&fileId=" + fileId;
							   });
							   
							   $("img[data-from='me']").each(function(i){  
							        this.src = ctx+"/user/file.action?method:previewPic&fileId=" + fileId;
							   });
							  
							});
		 	         
	            	 $( this ).dialog( "close" );
	               	 return false;
	               },
	               "取消上传": function() {
	                   $( this ).dialog( "close" );
	                   return false;
	               }                           
	           }
		});
	});


}
function fileSelected() {
	var file = document.getElementById('sampleFile').files[0];
	if (file) {
		var fileName = file.name;
		var postFix = fileName.match(/(\S*)\.(\S*)$/)[2];
			
	    if( postFix =='bmp' || postFix =='jpeg' || postFix =='gif' || postFix =='jpg' || postFix =='png'){  //如果是图片的话
			
	    	performAjaxSubmit();
		
	    } else { //如果不符合要求的情况
			if (window.ActiveXObject) {//for IE
				file.select(); //select the file ,and clear selection
				document.selection.clear();
			} else if (window.opera) { //for opera
				file.type = "text";
				file.type = "file";
			} else
				file.value = ""; //for FF,Chrome,Safari
			alert("您选择的文件非图片格式，请重新选择文件。。。。。。");
		}
	}
}

function uploadProgress(evt) {
	if (evt.lengthComputable) {
		var percentComplete = Math.round(evt.loaded * 100 / evt.total);
		document.getElementById('progressNumber').innerHTML = '<Strong>上传进度显示:</Strong>'+ percentComplete.toString() + '%' +"<br>";
	} else {
		document.getElementById('progressNumber').innerHTML = 'unable to compute';
	}
}

function performAjaxSubmit() {

	var sampleFile = document.getElementById("sampleFile").files[0];
	var formdata = new FormData();
	formdata.append("sampleFile", sampleFile);
	var xhr = new XMLHttpRequest();
	xhr.upload.addEventListener("progress", uploadProgress, false);
	xhr.open("POST", ctx+"/user/file.action?method:uploadFile=xx", true);
	xhr.send(formdata);
	xhr.onload = function(e) {
		if (this.status == 200) {
			eval("var json=" + this.responseText);
			
			 //将当前头像文件的Id隐藏
	    	 $("#data-avatar-hidden").attr("data-avatar-hidden",json.fileId);  
			
			 //将图片显示出来
			 $("#OriPreviewPic").append("<img style='max-width:450px;' src='"+ctx+"/user/file.action?method:previewPic&fileId=" + json.fileId + "' ><img>");
			
		     //将头像显示出来
		     $("#avatarPreviewPic").append("<img style='max-width:110px;' src='"+ctx+"/user/file.action?method:previewPic&fileId=" + json.fileId + "' ><img>");
		}
	};
}